<?php

class Db_Joins_TablesJoins
{
	var $keyword;
	var $member_id;
	var $od = 1; //1=product_id 2=sale_in_30_days
	var $cid;
	var $page_id = 1;
	var $page_size = 40;
	var $product_id;
	
	function __construct() {
		$this->db = Zend_Registry::get("db");
	}
	
	function GetProducts()
	{
		$result = array(
			'total_pages' => 0,
			'current_page_info' => array()
		);
		
		if($this->member_id)
		{
			//count total for pagination
			$data = $this->db->select();
			$data->from("bjm_members_link as l", array("count(lid) as ct"));
			$data->joinLeft("bjm_members_link_type as t", "t.mtid=l.mtid", array());
			$data->joinLeft("bjm_products as p", "p.product_id=l.product_id", array());
			$data->where("l.short_link is not null");
			$data->where("l.short_link != ?", "");
			$data->where("t.member_id = ?", $this->member_id);
			$data->where("t.type_name = ?", "默认");
			$data->where("t.status = ?", 1);
			$data->where("p.coupon_expired_date >= ?", date("Y-m-d"));
			
			if($this->cid)
			{
				$data->where("p.cid = ?", $this->cid);
			}

			if($this->keyword)
			{
				$data->where("p.taobao_id like '%".$this->keyword."%' or p.short_title like '%".$this->keyword."%' or p.long_title like '%".$this->keyword."%' or p.short_desc like '%".$this->keyword."%'");
			}

			$ct = $this->db->fetchRow($data);

			//dump all in condition
			$data = $this->db->select();
			$data->from("bjm_members_link as l", array());
			$data->joinLeft("bjm_members_link_type as t", "t.mtid=l.mtid", array());
			$data->joinLeft("bjm_products as p", "p.product_id=l.product_id", array("product_id", "pic_local", "short_title", "coupon_value", "sale_price", "coupon_url", "sale_in_30_days", "upload_by"));
			$data->where("l.short_link is not null");
			$data->where("l.short_link != ?", "");
			$data->where("t.member_id = ?", $this->member_id);
			$data->where("t.type_name = ?", "默认");
			$data->where("t.status = ?", 1);
			$data->where("p.coupon_expired_date >= ?", date("Y-m-d"));
			
			if($this->cid)
			{
				$data->where("p.cid = ?", $this->cid);
			}

			if($this->keyword)
			{
				$data->where("p.taobao_id like '%".$this->keyword."%' or p.short_title like '%".$this->keyword."%' or p.long_title like '%".$this->keyword."%' or p.short_desc like '%".$this->keyword."%'");
			}

			if($this->page_id)
			{
				$data->limit($this->page_size, $this->page_size * ($this->page_id - 1));
			}
			
			if(1 == $this->od)
			{
				$data->order(array("p.product_id DESC"));
			}else{
				$data->order(array("p.sale_in_30_days DESC"));
			}

			$origine = $this->db->fetchAll($data);
			
			$current_page_info = array();

			if(!empty($origine))
			{
				$product_ids = array();

				$current_page_info = array(
					"product_id" => array(),
					"pic_local" => array(),
					"short_title" => array(),
					"coupon_value" => array(),
					"sale_price" => array(),
					"coupon_url" => array(),
					"sale_in_30_days" => array(),
					"upload_by" => array()
				);

				foreach($origine as $row)
				{
					$product_ids[] = $row['product_id'];

					$current_page_info['short_link'][$row['product_id']] = $row['short_link'];
					$current_page_info['tao_token'][$row['product_id']] = $row['tao_token'];
					$current_page_info['pic_local'][$row['product_id']] = $row['pic_local'];
					$current_page_info['short_title'][$row['product_id']] = $row['short_title'];
					$current_page_info['coupon_value'][$row['product_id']] = $row['coupon_value'];
					$current_page_info['sale_price'][$row['product_id']] = $row['sale_price'];
					$current_page_info['coupon_url'][$row['product_id']] = $row['coupon_url'];
					$current_page_info['sale_in_30_days'][$row['product_id']] = $row['sale_in_30_days'];
					$current_page_info['product_id'][$row['product_id']] = $row['product_id'];
					$current_page_info['upload_by'][$row['product_id']] = $row['upload_by'];
				}
			}
			
			$result = array(
				'total_pages' => ceil($ct['ct'] / $this->page_size),
				'current_page_info' => $current_page_info
			);
		}
		
		return $result;
	}
	
	function GetProductDetails()
	{
		$result = NULL;
		
		if($this->product_id && $this->member_id)
		{
			$mod_link_type = new Db_Tables_BjmMembersLinkType();
			$mod_link_type->member_id = $this->member_id;
			$mtid = $mod_link_type->GetDefaultId();
			
			$select = $this->db->select();
			$select->from("bjm_products as p", array("product_id", "platform", "taobao_id", "short_title", "long_title", "original_price", "sale_price", "short_desc", "pic", "sale_in_30_days", "coupon_value", "coupon_expired_date", "coupon_used_qty", "coupon_min_amount_to_use", "coupon_max_use_qty", "product_url", "coupon_url", "pic_local", "upload_by"));
			$select->joinLeft("bjm_members_link as l", "l.product_id=p.product_id", array("short_link"));
			$select->where("p.product_id = ?", $this->product_id);
			$select->where("l.mtid = ?", $mtid);
			$row = $this->db->fetchRow($select);
			
			if($row)
			{
				$result = array(
					"product_id" => $row['product_id'],
					"platform" => $row['platform'],
					"taobao_id" => $row['taobao_id'],
					"short_title" => $row['short_title'],
					"long_title" => $row['long_title'],
					"original_price" => $row['original_price'],
					"sale_price" => $row['sale_price'],
					"short_desc" => $row['short_desc'],
					"pic" => $row['pic'],
					"sale_in_30_days" => $row['sale_in_30_days'],
					"coupon_value" => $row['coupon_value'],
					"coupon_expired_date" => $row['coupon_expired_date'],
					"coupon_used_qty" => $row['coupon_used_qty'],
					"coupon_min_amount_to_use" => $row['coupon_min_amount_to_use'],
					"coupon_max_use_qty" => $row['coupon_max_use_qty'],
					"product_url" => $row['product_url'],
					"coupon_url" => $row['coupon_url'],
					"short_link" => $row['short_link'],
					"pic_local" => $row['pic_local'],
					"upload_by" => $row['upload_by']
				);
			}
			
			//generate tao_token
			$mod_pid = new Db_Tables_BjmMembersPid();
			$pid = $mod_pid->GetPid($this->member_id);
			
			if($pid[2])
			{
				$mod_ext = new Sys_Extensions();
				$tao_token = $mod_ext->GetTaoToken($result['coupon_url'], $result['product_url'], $pid[2], $row['short_title']);
				$result['tao_token'] = $tao_token;
			}
		}
		
		return $result;
	}
}